package be.nvlooy.moviereminder.common;

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.io.*;

public class ImageResizer {
	
	public static BufferedImage scale(BufferedImage image, int targetWidth, int targetHeight) throws IOException {
		BufferedImage bdest = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bdest.createGraphics();
		
		AffineTransform at = AffineTransform.getScaleInstance(
				(double) targetWidth / image.getWidth(), 
				(double) targetHeight / image.getHeight());
		g.drawRenderedImage(image, at);
		
		return bdest;
	}
	
	public static BufferedImage scaleFit(BufferedImage image, int targetWidth, int targetHeight) {
		int orgWidth = image.getWidth();
		int orgHeight = image.getHeight();
		double scale;

		double scalex = (double) targetWidth / orgWidth;
		double scaley = (double) targetHeight / orgHeight;
		scale = Math.max(scalex, scaley);
		
		AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
		
		BufferedImage bdest = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bdest.createGraphics();
		g.drawRenderedImage(image, at);
		
		return bdest;
	}
	
}